// $License: NOLICENSE
//--------------------------------------------------------------------------------
/**
  @file $relPath
  @copyright $copyright
  @author Administrator
*/

//--------------------------------------------------------------------------------
// Libraries used (#uses)

//--------------------------------------------------------------------------------
// Variables and Constants
global mapping threadId;
//global bool soundSW1 = 1;
//global bool soundSW2 = 1;
//--------------------------------------------------------------------------------
/**
*/
main()
{
  dpConnect("jumpCB1", "CR:F2_CR_FAB_4_PLC01_CONNECT.STATE.CLOCK.VALUE");
  //dpConnect("jumpCB2", "CR:F2_CR_FAB_4_PLC02_CONNECT.STATE.CLOCK.VALUE");
  string key = "F2_CR_PLC"; //定义key值；
  synchronized(threadId)
  {
    if (mappingHasKey(threadId, key))
      stopThread(threadId[key]);

    threadId[key] = startThread("repeat");//开启线程；
  }
}
//计算时间差
private long _timeDiffMillisecond(time a, time b)
{
  return _timeMillisecond(b) - _timeMillisecond(a);
}

private long _timeMillisecond(time t)
{
  long millisecondTime = 0;

  millisecondTime += milliSecond(t);
  millisecondTime += second(t) * 1000;
  millisecondTime += minute(t) * 1000 * 60;
  millisecondTime += hour(t) * 1000 * 60 * 60;

  return millisecondTime;
}

time oldTime1;
time oldTime2;
time current;
long tt1;
long tts1;
long tt2;
long tts2;

void jumpCB1(string dp1)
{
  oldTime1 = getCurrentTime();
  DebugTN("oldTime1=" + (string) oldTime1);

}

void jumpCB2(string dp1)
{
  oldTime2 = getCurrentTime();
  DebugTN("oldTime2=" + (string) oldTime2);

}

void repeat()
{
  while (true)
  {
    delay(1);
    current = getCurrentTime();
    DebugTN("currentTime=" + (string)current);
    tt1 = _timeDiffMillisecond(oldTime1, current);
    tts1 = tt1 / 1000;
    tt2 = _timeDiffMillisecond(oldTime2, current);
    tts2 = tt2 / 1000;
    DebugN("PLC01秒差：" + tts1);
    //DebugN("PLC02秒差：" + tts2);

    if (tts1 > 10)
    {
      dpSet("CR:F2_CR_FAB_4_PLC01_CONNECT.ERROR.offLine.VALUE", 1);
      DebugN("PLC1超时");

//       if (soundSW1)
//       {
//         system("start /b " + getPath(BIN_REL_PATH, "tts_vc.exe") + " PLC01掉线！");
//         delay(2);
//       }

    }
    else
    {
      dpSet("CR:F2_CR_FAB_4_PLC01_CONNECT.ERROR.offLine.VALUE", 0);

    }

//     if (tts2 > 10)
//     {
//       dpSet("CR:F2_CR_FAB_4_PLC02_CONNECT.ERROR.offLine.VALUE", 1);
      //DebugN("PLC2超时");
//
//       if (soundSW2)
//       {
//         system("start /b " + getPath(BIN_REL_PATH, "tts_vc.exe") + " PLC02掉线！");
//         delay(2);
//       }
//     }
//     else
//     {
//       dpSet("CR:F2_CR_FAB_4_PLC02_CONNECT.ERROR.offLine.VALUE", 0);
//
//     }

  }


  //synchronized(threadId)
  //{
  //  mappingRemove(threadId, key);
// }
}

